function [gradients, loss, state, acc] = modelGradients(X,Y,parameters,state)

	% Execute the model function.
	isTraining = true;
	[YPred,state,dlT] = pointnetClassifier(X,parameters,state,isTraining);

	% Add regularization term to ensure feature transform matrix is
	% approximately orthogonal.
	K = size(dlT,1);
	B = size(dlT, 4);
	I = repelem(eye(K),1,1,1,B);
	dlI = dlarray(I,"SSCB");
	treg = mse(dlI,pagemtimes(dlT,permute(dlT,[2 1 3 4])));
	factor = 0.001;

	% Compute the loss.
	loss = crossentropy(YPred,Y) + factor*treg;

	% Compute the parameter gradients with respect to the loss. 
	gradients = dlgradient(loss, parameters);

	% Compute training accuracy metric.
	[~,YTest] = max(Y,[],1);
	[~,YPred] = max(YPred,[],1);
	acc = gather(extractdata(sum(YTest == YPred)./numel(YTest)));

end
